<template>
  <div class="box">
    <div class="querybox">
      <!-- <el-input v-model="query" placeholder="请输入查询内容" style="width: 200px; right: 10px;" /> -->
      <!-- <el-button type="primary" @click="productList">查询</el-button> -->
      <el-button
        type="primary"
        @click="add"
      >新增</el-button>
    </div>
    <el-table
      :data="tableData"
      border
    >
      <el-table-column
        prop="name"
        label="产品名称"
      />
      <el-table-column
        prop="code"
        label="产品代码"
      />
      <el-table-column
        prop="cover"
        label="产品封面"
      >
        <template slot-scope="scope">
          <img
            :src="scope.row.cover"
            alt=""
            width="80px"
          >
        </template>
      </el-table-column>
      <el-table-column
        prop="promotion"
        label="促销语"
      />
      <el-table-column
        prop="price"
        label="产品价格"
      />
      <el-table-column label="数量" width="80">
        <template slot-scope="scope">
          <div style="display: flex; justify-content: space-between;">
            <span style="display: inline-block;">{{ getVal(scope.row) }}</span>
            <!-- <span style="color: #778888;">{{ scope.row.type === 1 ? ' 次' : ' 天' }}</span> -->
            <el-tag size="mini">{{ scope.row.type === 1 ? ' 次' : ' 天' }}</el-tag>
          </div>
        </template>
      </el-table-column>
      <el-table-column
        prop="status"
        label="产品状态"
      />
      <el-table-column
        prop="level"
        label="产品等级"
      />
      <!-- <el-table-column
        label="状态"
      >
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.status"
            active-text="启用"
            inactive-text="禁用"
            @change="statusChange(scope.row)"
          />
        </template>
      </el-table-column> -->
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            type="text"
            size="small"
            @click="viewUser(scope.row.id, scope.index)"
          >查看</el-button>
          <!-- <el-button type="text" size="small" @click="updUser(scope.row, scope.index)">修改</el-button> -->
          <Del-tip
            type="text"
            size="small"
          >
            <el-button type="primary" size="mini" @click="delUser(scope.row.id)">确定</el-button>
          </Del-tip>
        </template>
      </el-table-column>
    </el-table>
    <el-row style="margin-top: 10px;">
      <el-pagination
        :total="total"
        :current-page="filters.pageNum"
        :page-size="filters.pageSize"
        layout="total, prev, pager, next, jumper"
        @size-change="handleSizeChange"
        @current-change="handleSizeChange"
      />
    </el-row>

    <el-dialog
      :title="editType === 'add' ? '新增' : '查看'"
      :visible.sync="dialogFormVisible"
      :before-close="beforeClose"
    >
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        :inline="true"
      >
        <el-form-item
          label="产品名称"
          prop="name"
          :label-width="formLabelWidth"
        >
          <el-input
            v-model="form.name"
            autocomplete="off"
          />
        </el-form-item>
        <el-form-item
          label="产品代码"
          prop="code"
          :label-width="formLabelWidth"
        >
          <el-input
            v-model="form.code"
            autocomplete="off"
          />
        </el-form-item>
        <el-form-item
          label="产品封面"
          prop="cover"
          :label-width="formLabelWidth"
        >
          <Upload
            v-model="form.cover"
            style="width: 185px;"
            :img-url="form.cover"
            @on-success="onSuccess"
          />
        </el-form-item>
        <el-form-item
          label="促销语"
          prop="promotion"
          :label-width="formLabelWidth"
        >
          <el-input
            v-model="form.promotion"
            autocomplete="off"
          />
        </el-form-item>
        <!-- <el-form-item label="logo" prop="logo" :label-width="formLabelWidth">
            <Upload v-model="form.logo" style="width: 185px;" :props-val="form.logo" @on-success="onSuccess" />
          </el-form-item> -->
        <el-form-item
          label="产品价格"
          prop="price"
          :label-width="formLabelWidth"
        >
          <el-input
            v-model="form.price"
            autocomplete="off"
          />
        </el-form-item>
        <el-form-item
          label="产品状态"
          prop="status"
          :label-width="formLabelWidth"
        >
          <el-input
            v-model="form.status"
            autocomplete="off"
          />
        </el-form-item>
        <el-form-item
          label="产品等级"
          prop="level"
          :label-width="formLabelWidth"
        >
          <el-input
            v-model="form.level"
            autocomplete="off"
          />
        </el-form-item>
        <el-form-item
          label="产品类型"
          prop="type"
          :label-width="formLabelWidth"
        >
          <el-select
            v-model="form.type"
            placeholder="请选择"
            style="width: 185px;"
          >
            <el-option
              key="134"
              label="次数"
              :value="1"
            />
            <el-option
              key="32234"
              label="时间"
              :value="2"
            />
          </el-select>
        </el-form-item>
        <!-- <el-form-item label="商业机构" prop="bizOrg" :label-width="formLabelWidth">
            <el-switch
              v-model="form.bizOrg"
              @change="bizOrgChange(form.id)"
              :disabled="form.bizOrg">
            </el-switch>
          </el-form-item> -->
      </el-form>
      <!-- <el-form ref="form" :model="form" :rules="rules">
        <el-form-item label="姓名" prop="username" :label-width="formLabelWidth">
          <el-input v-model="form.username" autocomplete="off" />
        </el-form-item>
        <el-form-item label="电话" prop="mobile" :label-width="formLabelWidth">
          <el-input v-model="form.mobile" autocomplete="off" />
        </el-form-item>
      </el-form> -->
      <div
        slot="footer"
        class="dialog-footer"
      >
        <el-button @click="resetForm('form')">取 消</el-button>
        <el-button
          type="primary"
          @click="closeDialogForm('form')"
        >确 定</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { productList, productInfo, productEdit, productDelete } from '@/api/product'
import { bizorg_trans } from '@/api/org'
import { timeTranslateHMS } from '@/utils/formatdate'
import Upload from '@/components/Upload'
export default {
  name: 'Documentation',
  components: {
    Upload
  },
  data() {
    return {
      timeTranslateHMS,
      filters: {
        mobile: '',
        username: '',
        pageNum: 1,
        pageSize: 10
      },
      form: {
        name: '',
        code: '',
        cover: '',
        promotion: '',
        price: '',
        status: '',
        level: '',
        type: '',
        logo: ''
      },
      total: 0,
      rules: {
        name: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
        cover: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
        promotion: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
        price: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
        status: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
        level: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
        type: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
        logo: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ]
      },
      tableData: [],
      formLabelWidth: '120px',
      dialogFormVisible: false,
      query: '',
      editType: null
    }
  },
  mounted() {
    this.productList()
  },
  methods: {
    getVal(row) {
      if (row.type === 2) {
        return row.value / 86400
      } else {
        return row.value
      }
    },
    bizOrgChange(orgId) {
      orgId = '4028808a748b6e4f01748b71b6bf0002'
      bizorg_trans({ orgId }).then((res) => {
      })
    },
    // dialog关闭事件
    beforeClose() {
      this.resetForm('form')
    },
    // logo上传成功监听
    onSuccess(res) {
      this.form.cover = res
    },
    // 新增操作
    add() {
      this.editType = 'add'
      this.dialogFormVisible = true
    },
    // 分页切换监听
    handleSizeChange(pageNum) {
      this.filters.pageNum = pageNum
      this.productList()
    },
    // 用户列表
    productList() {
      const pageNum = this.filters.pageNum - 1
      productList({ ...this.filters, pageNum }).then(res => {
        const result = res.data
        this.tableData = result.content
        this.total = result.totalElements
      }).catch(err => {
        console.log(err)
      })
    },
    // 删除用户
    delUser(productId) {
      this.editType = 'delete'
      productDelete({ productId }).then((res) => {
        this.$message({
          message: '删除成功！',
          type: 'success'
        })
        this.carestoreList()
      })
    },
    // 修改用户
    updUser(row, index) {
      this.editType = 'update'
      this.dialogFormVisible = true
    },
    // 查看用户
    viewUser(id) {
      this.editType = 'view'
      this.dialogFormVisible = true
      this.productInfo(id)
    },
    // 用户详情
    productInfo(id) {
      productInfo({ productId: id }).then(res => {
        // res.data.org.logo = 'https://dss1.baidu.com/70cFfyinKgQFm2e88IuM_a/forum/pic/item/bd315c6034a85edf3d432b7d44540923dc5475a5.jpg'
        this.form = res.data
        // let result = res.data;
        // this.tableData = result.users;
      }).catch(err => {
        console.log(err)
      })
    },
    // 弹窗关闭触发函数
    closeDialogForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.productEdit(formName)
        } else {
          return false
        }
      })
    },
    resetForm(formName) {
      this.dialogFormVisible = false
      this.form = {}
      this.$refs[formName].resetFields()
    },
    // 新增用户
    productEdit(formName) {
      const form = this.form
      productEdit(form).then((res) => {
        this.$message({
          message: `${this.editType === 'add' ? '新增' : '修改'}成功！`,
          type: 'success'
        })
        this.resetForm(formName)
        this.productList()
        // this.dialogFormVisible = false
      })
    },
    statusChange(row) {
      // {id: row.id, status: row.status}
    }
  }
}
</script>

<style lang="scss" scoped>
.box {
  margin: 20px 30px;
  .form {
    margin-top: 50px;
  }
  .querybox {
    text-align: right;
    margin-bottom: 20px;
  }
}
.box-card {
  margin-bottom: 15px;
}
</style>
